Skip to content

IsWindowVisible() may produce an error#2171

Open
Enyium wants to merge 1 commit intoMicrosoftDocs:docsfrom
Enyium:fix-iswndvisible
Open

IsWindowVisible() may produce an error#2171
Enyium wants to merge 1 commit intoMicrosoftDocs:docsfrom
Enyium:fix-iswndvisible

Conversation

@Enyium
Copy link

@Enyium Enyium commented Feb 1, 2026

I was hesitant to commit the following text, which was based on tests, because it may sound like an unauthorized contract:

To be able to tell an absent WS_VISIBLE style apart from an error condition and retrieve extended error information, call SetLastError(ERROR_SUCCESS) in preparation to IsWindowVisible, and finally GetLastError.

(As Markdown)
To be able to tell an absent <b>WS_VISIBLE</b> style apart from an error condition and retrieve extended error information, call <b><a href="/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror">SetLastError(ERROR_SUCCESS)</a></b> in preparation to <b>IsWindowVisible</b>, and finally <b><a href="/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror">GetLastError</a></b>.

Or, a bit stronger:

To get extended error information, call GetLastError. To be able to tell an absent WS_VISIBLE style apart from an error condition, call SetLastError(ERROR_SUCCESS) in preparation to IsWindowVisible.

(As Markdown)
To get extended error information, call <b><a href="/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror">GetLastError</a></b>. To be able to tell an absent <b>WS_VISIBLE</b> style apart from an error condition, call <b><a href="/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror">SetLastError(ERROR_SUCCESS)</a></b> in preparation to <b>IsWindowVisible</b>.

Would it be okay to use one of those texts as the second paragraph under the "Return value" heading?


Tests I did on Windows 11 Home 24H2 (in another language, from memory):

SetLastError(ERROR_SUCCESS);
isVisible = IsWindowVisible((HWND)123);  // Invalid handle.
if (!isVisible) {
    DWORD lastError = GetLastError();
    // `lastError` is `ERROR_INVALID_WINDOW_HANDLE`.
}

///////////////////////////////////////

SetLastError(123);
isVisible = IsWindowVisible(validHwnd);
if (!isVisible) {
    DWORD lastError = GetLastError();
    // `lastError` is 123. So, preparation `SetLastError(ERROR_SUCCESS)` is needed.
}

Passing in garbage makes `GetLastError()` return `ERROR_INVALID_WINDOW_HANDLE`.
@prmerger-automator
Copy link

@Enyium : Thanks for your contribution! The author(s) and reviewer(s) have been notified to review your proposed change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants